/*******************************************************************************

PooledRegions analysis_travelcost.do

2018.09.06 (DJ) Created

This do file creates sum stats for transportation costs on different routes.

*******************************************************************************/



use "${analysis}/Kilimanjaro rq_census_mt_vt_google.dta", clear
	rename  (district ward village_name market) (survey_district survey_ward survey_village primary_market)
	
	append using "${analysis}/Manyara rq_census_mt_vt_google.dta"
	
	cap drop village_id
	egen village_id = group(survey_region survey_district survey_ward survey_village)
	
	foreach unit in km hrs{
		//gen MT_mkt_turnoff_cost_per`unit' 	= MT_mkt_cost_USD/google_vil_mkt_`unit'
		//gen MT_mkt_turnoff_cost_per`unit' 	= MT_mkt_cost_USD/RQ_rural_`unit'
		gen google_vil_mkt_cost_per`unit' 		= VT_vil_mkt_cost_USD/google_vil_mkt_`unit'
		gen RQ_vil_mkt_cost_per`unit' 			= VT_vil_mkt_cost_USD/RQ_rural_`unit'
	}
	
	
	// calculate the costs of a round-trip (2 ways) + carrying input/output (assumed to be equivalent to a 1 way fare).
	foreach var of varlist MT_mkt_*_cost_USD VT_vil_*_cost_USD RQ_vil_mkt_cost_USD RQ_rural_cost_USD{
		gen `var'_3ways = `var'*3
	}
	tempfile master
	save `master'

	
	
/*******************************************************************************
	TRAVEL COSTS on MAJOR ROADS
*******************************************************************************/

// MARKETS TO CITIES
	estimates clear
	use `master', clear
		drop MT_mkt_vil_cost_USD* MT_mkt_vil_hrs *nearcity* *turnoff* 
		keep MT_mkt_*_hrs survey_region primary_market MT_mkt_*_cost_USD* google_mkt_*_km google_mkt_*_hrs
		duplicates drop
		
		reshape long MT_mkt@_hrs MT_mkt@_cost_USD MT_mkt@_cost_USD_3ways google_mkt@_km google_mkt@_hrs, i(primary_market) j(city) string
		
		gen google_mkt_min 	= google_mkt_hrs*60
		gen MT_mkt_min 		= MT_mkt_hrs * 60
		
		//foreach dep in MT_mkt_cost_USD_3ways MT_mkt_hrs{
		foreach dep in MT_mkt_cost_USD MT_mkt_hrs{
		
		
			foreach indep in google_mkt_km google_mkt_hrs{		
		
				//eststo: xi: reg `dep' `indep' i.survey_region, nocons cluster(primary_market)
				eststo: xi: reg `dep' `indep', nocons cluster(primary_market)
				distinct primary_market if e(sample)
				estadd local numM = `r(ndistinct)'
			}
		}
		
		esttab, se replace compress nogap sca("N Observations" "numM Markets" "numV Villages")

	
	
/*******************************************************************************
	TRAVEL COSTS on RURAL ROADS (VILLAGES TO PRIMARY MARKETS)
	
	We have two options
	(1) use costs from Village Transport surveys
	(2) use costs from RQ surveys

*******************************************************************************/	

	use `master', clear
	
	//foreach dep in RQ_rural_cost_USD_3ways RQ_rural_hrs VT_vil_mkt_cost_USD_3ways VT_vil_mkt_hrs{
	foreach dep in RQ_rural_cost_USD RQ_rural_hrs VT_vil_mkt_cost_USD VT_vil_mkt_hrs{
	
		foreach indep in google_vil_mkt_km google_vil_mkt_hrs{
	
			//eststo: xi: reg `dep' `indep' i.survey_region, nocons cluster(village_id)
			eststo: xi: reg `dep' `indep', nocons cluster(village_id)
			distinct village_id if e(sample)
			estadd local numV = `r(ndistinct)'
		}
	}
		
	*/
	
	esttab, se replace compress nogap sca("N Observations" "numM Markets" "numV Villages") ///
			star(* 0.10 ** 0.05 *** 0.01) b(a3)
			
	
	
/*******************************************************************************
	TRANSPORTATION COSTS FOR FARMER HIM/HERSELF, and CARRYING INPUTS/OUTUTS
*******************************************************************************/		
	
	use "${analysis}/Manyara Farmer_mktaccess.dta", clear
	rename (survey_district survey_ward survey_village) (district ward village_name)
	drop secf*
	append using "${analysis}/Kilimanjaro Farmer_mktaccess.dta"
	
	cap drop village_id
	egen village_id = group(survey_region district ward village_name)

	// should these regressions be run at the FARMER level or VILLAGE level? 
	foreach dep in travel_carry17_av_50kg_USD_w1 travel_self17_av_USD_w1{
		foreach indep in google_farmer_AVvil_km google_farmer_AVvil_hrs{
		
			//eststo: xi: reg `dep' `indep' i.survey_region, nocons cluster(village_id)
			eststo: xi: reg `dep' `indep', nocons cluster(village_id)
		
			distinct village_id if e(sample)
			estadd local numV = `r(ndistinct)'
		}
	}
	
	foreach dep in travel_carry16_maize_USD_w1 travel_self16_maize_USD_w1{
		foreach indep in google_farmer_AVvil_km google_farmer_AVvil_hrs{
		
			//eststo: xi: reg `dep' `indep' i.survey_region, nocons cluster(village_id)
			eststo: xi: reg `dep' `indep', nocons cluster(village_id)
		
			distinct village_id if e(sample)
			estadd local numV = `r(ndistinct)'
		}
	}
	esttab, se(a1) b(2) se replace compress nogap star(* 0.10 ** 0.05 *** 0.01) sca("N Observations" "numV Villages") 
	
	#d;
	esttab using "${pool_results}/PooledRegions travelcost.csv", 
		se(2) b(2) 
		sca("N Observations" "numM Markets" "numV Villages")
		sfmt("%9.0g" "%9.0g" "%9.0g")
		replace compress noobs nogap star(* 0.10 ** 0.05 *** 0.01)
	;
	#d cr
	
	
	// estimate the standard error of 69% (1.28/1.83)
	
		eststo mod1: reg travel_carry17_av_50kg_USD_w1 google_farmer_AVvil_hrs, nocons
		eststo mod2: reg travel_self17_av_USD_w1 google_farmer_AVvil_hrs, nocons
		suest mod1 mod2, cluster(village_id)
		nlcom (ratio1: [mod1_mean]_b[google_farmer_AVvil_hrs]/[mod2_mean]_b[google_farmer_AVvil_hrs])
		
	exit
